package com.agou.app.admin.business.user.dao;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import org.hibernate.Query;

import com.agou.app.admin.dbconfig.bean.TSAdminUser;
import com.agou.app.admin.dbconfig.bean.TSAdminUserLog;
import com.agou.app.admin.dbconfig.bean.TSAdminUserSecurity;
import com.agou.core.ss.SSException;
import com.agou.core.ss.SSTrace;

public class UserDao extends IUserDao {

	private static SSTrace ssTrace = SSTrace.getInstance(UserDao.class);

	public TSAdminUserSecurity queryUserSecurity(long userId)
			throws SSException, SQLException, IOException {
		StringBuilder sbSql = new StringBuilder();
		sbSql.append("select t from TSAdminUserSecurity t ");
		sbSql.append(" where t.userId = ?");
		Query query = this.getJdbcSession().createQuery(sbSql.toString());
		query.setParameter(0, userId);
		// 对象查询条件
		@SuppressWarnings("unchecked")
		List<Object> reList = query.list();
		if (reList != null && reList.size() > 0) {
			return (TSAdminUserSecurity)reList.get(0);
		}else{
			return null;
		}
	};

	public TSAdminUser queryUserById(long userId) 
			throws SSException, SQLException, IOException {
		StringBuilder sbSql = new StringBuilder();
		sbSql.append("select t from TSAdminUser t ");
		sbSql.append(" where t.userId = ?");
		Query query = this.getJdbcSession().createQuery(sbSql.toString());
		query.setParameter(0, userId);
		// 对象查询条件
		@SuppressWarnings("unchecked")
		List<Object> reList = query.list();
		if (reList != null && reList.size() > 0) {
			return (TSAdminUser)reList.get(0);
		}else{
			return null;
		}
	}

	public TSAdminUser queryUserByUserLoginId(String userLoginId)
			throws SSException, SQLException, IOException {
		StringBuilder sbSql = new StringBuilder();
		sbSql.append("select t from TSAdminUser t ");
		sbSql.append(" where t.userLoginId = ?");
		Query query = this.getJdbcSession().createQuery(sbSql.toString());
		query.setParameter(0, userLoginId);
		// 对象查询条件
		@SuppressWarnings("unchecked")
		List<Object> reList = query.list();
		if (reList != null && reList.size() > 0) {
			return (TSAdminUser)reList.get(0);
		}else{
			return null;
		}
	}

	// 保存用户
	public int saveUser(TSAdminUser adminUserVo) 
			throws SSException, SQLException, IOException {
		ssTrace.debug("saveUser", "保存用户：#0", adminUserVo);
		this.getJdbcSession().save(adminUserVo);// 保存用户信息
		return 1;
	}

	// 保存用户安全信息
	public int saveUserSecurity(TSAdminUserSecurity adminUserSecurityVo)
			throws SSException, SQLException, IOException {
		this.getJdbcSession().save(adminUserSecurityVo);// 保存用户信息
		return 1;
	}

	// 保存用户日志
	public int saveUserLog(TSAdminUserLog adminUserLogVo) throws SSException,
			SQLException, IOException {
		this.getJdbcSession().save(adminUserLogVo);// 保存用户信息
		return 1;
	}

}
